package com.tvein.sanguo.room.controller
{
	//flash
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import com.tvein.sanguo.room.common.GameConstants;
	import com.tvein.sanguo.room.model.SystemProxy;
	import com.tvein.sanguo.room.view.RoomMediator;
//
	public class RoomBuild extends SimpleFabricationCommand implements ICommand
	{
		private var systemData:SystemProxy;
		private var roomMediator:RoomMediator;
		
		override public function execute(notification:INotification) : void
		{
			systemData = facade.retrieveProxy(SystemProxy.NAME) as SystemProxy;
			roomMediator = facade.retrieveMediator(RoomMediator.NAME) as RoomMediator;
			
			roomMediator.roomShell.clearButton();
			
			var i:int;
			var rooms:Array;
			var params:Array;
			var roomButton:Object = new Object();
			
			//RoomParent
			if (notification.getBody()[0] != null)
			{
				rooms = notification.getBody()[0].split(",");
				
				for (i = 0; i < rooms.length - 1; i++)
				{
					params = rooms[i].split(":");
					
					roomButton.Name = "离开";
					roomButton.Type = 0;
					roomButton.CMD = "RoomChange(" + params[0] + ");";
					roomButton.isVisible = false;
					
					roomMediator.roomShell.console("addButton", roomButton);
					checkRoomFile(params[0], "离开", params[2], params[3]);
				}
			}
			
			//RoomChild
			if (notification.getBody()[1] != null)
			{
				rooms = notification.getBody()[1].split(",");
				
				for (i = 0; i < rooms.length - 1; i++)
				{
					params = rooms[i].split(":");
					
					roomButton.Name = params[1];
					roomButton.Type = 0;
					roomButton.CMD = "RoomChange(" + params[0] + ");";
					roomButton.isVisible = false;
					
					roomMediator.roomShell.console("addButton", roomButton);
					checkRoomFile(params[0], params[1], params[2], params[3]);
				}
			}
		}
		
		//Custom
		private function checkRoomFile(RoomID:String, RoomName:String, Music:String, BackGround:String):void
		{	
			var params:Array = RoomID.split("-");
			var RoomType:String;
			
			switch (params[0]) 
			{
				case "1":
					RoomType = "Canton";
					break;
				case "2":
					RoomType = "County";
					break;
				case "3":
					RoomType = "Fort";
					break;
				case "4":
					RoomType = "Place";
					break;
			}
			
			Music = "/SanGuo/sound/music/" + Music;
			BackGround = "/SanGuo/shell/background/" + RoomType + "/" + BackGround;
			
			var hasMusic:Boolean = false;
			var hasBackGround:Boolean = false;
			var i:int;
			
			for (i = 0; i < systemData.srcListGet().length; i++)
			{
				if (systemData.srcListGet()[i] == Music)
				{
					hasMusic = true;
					break;
				}	
			}
			
			for (i = 0; i < systemData.srcListGet().length; i++)
			{
				if (systemData.srcListGet()[i] == BackGround)
				{
					hasBackGround = true;
					break;
				}	
			}
			
			if (hasMusic && hasBackGround)
			{
				roomMediator.roomShell.console("setButtonVisible", {"buttonName":RoomName, "isVisible":true});
			}
			else
			{
				systemData.roomList = [RoomID, RoomName, Music, hasMusic, BackGround, hasBackGround];
			}
			
			if (!hasMusic)
			{
				routeNotification(GameConstants.SYSTEM_CONSOLE, ["-moduleFile", [GameConstants.MODULE_ROOM, Music]], null, "*");
			}
			
			if (!hasBackGround)
			{
				routeNotification(GameConstants.SYSTEM_CONSOLE, ["-moduleFile", [GameConstants.MODULE_ROOM, BackGround]], null, "*");
			}
		}
	}
}